package Gather.GatherExercise;

import java.util.ArrayList;

/**
 * ClassName:UserText002
 * Package:Gather.UserObjectGather
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/1/18-1:15
 * @Version:v1.0
 */
public class UserText002 {
    /*
     * main方法中定义一个集合,存入三个用户对象
     * 用户属性：id,username,password
     * 要求：定义一个方法,根据id查找对应的用户信息
     * 如果存在,返回索引
     * 如果不存在,返回 -1 */
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();

        //创建三个用户对象
        User u1 = new User("001", "zhangsan", "huawei");
        User u2 = new User("002", "lisi", "huawei");
        User u3 = new User("003", "wangwu", "huawei");
        //把用户对象添加到集合当中
        list.add(u1);
        list.add(u2);
        list.add(u3);
        int index = getIndex(list, "004");
        System.out.println(index);
    }

    public static int getIndex(ArrayList<User> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            String uid = u.getId();
            if (uid.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
